package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Order;
import net.suncaper.demo.domain.Product;
import net.suncaper.demo.domain.Shopcart;
import net.suncaper.demo.domain.User;
import net.suncaper.demo.service.OrderService;
import net.suncaper.demo.service.ProductService;
import net.suncaper.demo.service.ShopcartService;
import net.suncaper.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
@Controller
@RequestMapping
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private  ShopcartService shopcartService;
    @Autowired
    private ProductService productService;


    @RequestMapping("/searchorderByUid")
    @ResponseBody
    public List<Order> findStoreByUid(int userId){
        return orderService.findorderbyUid(userId);
    }

    @GetMapping("/order/orderall")
    public String orderPage(String userId, Model model){
        int userID = Integer.valueOf(userId).intValue();
        List<Order> orders=orderService.findorderbyUid(userID);
        model.addAttribute("orders",orders);
        return "pages/order/orderbyuser.html";

    }


    @GetMapping("/order/addToOrder")//单个下单
    public String orderPagebyuid(Integer shopcartId, Model model,String userAddress,String userPhone){
        System.out.println("当前下单的shopcartid："+shopcartId+"  用户地址："+userAddress+"  用户电话号码："+userPhone);
        Shopcart shopcart=shopcartService.findShopcartBySId(shopcartId);
        Order order=new Order();
        order.setUserId(shopcart.getUserId());
        order.setProductId(shopcart.getProductId());
        order.setOrderNum(shopcart.getShopcartNum());
        order.setOrderCreatetime(shopcart.getShopcartCreatetime());
        order.setOrderState(1);
        order.setOrderDiscount(1.0);
        order.setOrderAddress(userAddress);
        order.setOrderPhone(userPhone);
        order.setOrderPrice(shopcart.getShopcartPrice());
        order.setOrderSum(shopcart.getShopcartPrice()*shopcart.getShopcartNum());
        order.setProductName(shopcart.getProductName());
        orderService.saveAddOrderMes(order);

        Product product = productService.findProductById(order.getProductId());
        product.setProductNum(product.getProductNum()-order.getOrderNum());
        productService.updateProduct(product);
        shopcartService.delShopcartBySIdMes(shopcart.getShopcartId());
        model.addAttribute("orders",order);
        return "pages/user/index.html";
    }

    @GetMapping("/order/addAllToOrder")//购物车商品全部下单
    public String addAllToOrder(String userId, Model model,String userAddress,String userPhone){
        System.out.println("测试数据");
        System.out.println(userId);
        int userID = Integer.valueOf(userId).intValue();
        List<Shopcart> shopcarts=shopcartService.findShopcartByUId(userID);
        for(Shopcart shopcart:shopcarts){
            Order order = new Order();
            double temp =1;
            order.setOrderDiscount(temp);
            System.out.println("kanakk"+shopcart.getShopcartNum());
            order.setOrderNum(shopcart.getShopcartNum());
            order.setProductId(shopcart.getProductId());
            order.setOrderCreatetime(shopcart.getShopcartCreatetime());
            order.setOrderState(1);
            order.setOrderPrice(shopcart.getShopcartPrice());
            order.setOrderSum(shopcart.getShopcartPrice()*shopcart.getShopcartNum());
            order.setOrderNum(shopcart.getShopcartNum());
            order.setProductName(shopcart.getProductName());
            order.setOrderPhone(userPhone);
            order.setOrderAddress(userAddress);
            order.setUserId(userID);
            orderService.saveAddOrderMes(order);
            Product product = productService.findProductById(shopcart.getProductId());
            product.setProductNum(product.getProductNum()-shopcart.getShopcartNum());
            shopcartService.delShopcartBySIdMes(shopcart.getShopcartId());
            productService.updateProduct(product);
        }

        return "pages/user/index.html";
    }

    @GetMapping("/order/delOrder")//删除订单
    public String delPage(String orderId,Model model){
        int orderID = Integer.valueOf(orderId).intValue();
        model.addAttribute("order",orderService.findOrderByOId(orderID));
        return "pages/order/del-order.html";
    }
    @PostMapping("/order/delOrder")
    public String delStoreBySId(Order order){
        orderService.delOrderBySIdMes(order.getOrderId());
        return "pages/user/index.html";
    }




    @GetMapping("order/cancelOrder")//取消订单
    public  String cancelUserOrder(Integer orderId,Model model){
        //int orderID = Integer.valueOf(orderId).intValue();
        System.out.println("当前取消的订单id为："+orderId);
        Order order= orderService.findOrderByOId(orderId);
        order.setOrderState(0);
        orderService.saveUpdateOrderMes(order);
        return "pages/user/index.html";
    }

    @GetMapping("order/payOrder")//支付订单
    public  String payUserOrder(Integer orderId,Model model){
        System.out.println("当前进行支付的订单id为："+orderId);
        Order order= orderService.findOrderByOId(orderId);
        order.setOrderState(2);
        orderService.saveUpdateOrderMes(order);
        model.addAttribute("orders",order);
        return "pages/order/pay.html";
    }


}
